package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.HashUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.text.Convert;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.Customer;
import cn.openea.perpartner.meily.web.entity.PersonalInfo;
import cn.openea.perpartner.meily.web.entity.Provider;
import cn.openea.perpartner.meily.web.entity.dto.ProviderDTOFactory;
import cn.openea.perpartner.meily.web.entity.vo.ProviderVo;
import cn.openea.perpartner.meily.web.service.ICustomerService;
import cn.openea.perpartner.meily.web.service.IPersonalInfoService;
import cn.openea.perpartner.meily.web.service.IProviderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Provider)表控制层
 *
 * @author Weird077
 * @since 2023-01-18 19:29:50
 */
@Api(tags = "服务商管理")
@RestController
@RequestMapping("/provider")
public class ProviderController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private IProviderService providerService;
    @Resource
    private ProviderDTOFactory providerDTOFactory;

    @ApiOperation("服务商列表")
    @PostMapping("/list")
    public TableDataInfo selectList(@RequestBody Provider provider) {
        startPage();
        List<Provider> list = providerService.selectList(provider);
        return getDataTable(list);
    }

    @ApiOperation("导出服务商列表")
    @PostMapping("/list/export")
    public void exportList(@RequestBody ProviderVo providerVo, HttpServletResponse response) {
        List<Provider> providers;
        if (providerVo.byIds()) {
            // 根据id去查
            providers = providerService.findByIds(providerVo.getIds());
        } else {
            providers = providerService.selectList(providerVo.getProvider());
        }
        List<ProviderDTOFactory.ExportDTO> exportDTOList = providers
                .stream()
                .map(item -> providerDTOFactory.toExportDTO.apply(item))
                .collect(Collectors.toList());
        // 导出
        ExcelUtil<ProviderDTOFactory.ExportDTO> util = new ExcelUtil<>(ProviderDTOFactory.ExportDTO.class);
        util.exportExcel(response, exportDTOList, "区域服务商.xlsx");
    }

    @ApiOperation("服务商详情")
    @PostMapping("/info")
    public AjaxResult selectByUserId(@RequestBody Provider provider) {
        return success(this.providerService.getByUserId(provider.getUserId()));
    }

    @ApiOperation("服务商详情")
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable String id) {
        return success(this.providerService.getById(id));
    }

    @ApiOperation("地址匹配")
    @PostMapping("/match")
    public TableDataInfo match(@RequestBody Provider provider) {
        List<Provider> list = providerService.match(provider);
        return getDataTable(list);
    }

    @ApiOperation("修改服务商")
    @PostMapping("/edit/{id}")
    public AjaxResult edit(@RequestBody Provider provider, @PathVariable Long id) {
        provider.setId(id);
        providerService.editById(provider);
        return AjaxResult.success();
    }

    @ApiOperation("保存服务商")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Provider provider) {
        if(!this.providerService.add(provider)){
            return AjaxResult.error("保存失败！");
        }
        return AjaxResult.success("保存成功！");
    }

    @ApiOperation("删除服务商")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> idList) {
        return toAjax(this.providerService.removeByIds(idList));
    }

    @ApiOperation("修改服务商等级")
    @PostMapping("/updateGradeByUserId")
    public AjaxResult updateGradeByUserId(@RequestBody Provider provider) {
        return toAjax(this.providerService.updateGradeByUserId(provider));
    }
}

